package praktikumid.w09.p5;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

/**
 * Listener for the "draw oval" button. An example
 * how to use listener in a separate class in
 * a separate file.
 * 
 * NOTE: whenever a method returns an object,
 * you should check, whether this object 
 * might be null.
 * if (x.getParent() == null)
 * otherwise you might end up getting NullPointerException.
 * Another possibility would be to do proper exception
 * handling (not just put everything inside one general 
 * try-catch block).
 * @author Ago
 *
 */
public class DrawOvalListener implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		// called when button is clicked
		System.out.println("clicked");
		System.out.println(e.getSource());
		JButton b = (JButton) e.getSource();
		System.out.println();
		System.out.println(b.getParent().getParent().getParent().getParent().getParent());
		MyFrame f = (MyFrame) b.getParent().getParent().getParent().getParent().getParent();
		DrawPanel dp = (DrawPanel) f.getContentPane().getComponent(1);
		
		dp.drawOval(100, 100);
		// instead of traversing up to the frame
		// it would be enought to do the following
		// in order to get the DrawPanel
		System.out.println(b.getParent().getParent().getComponent(1));
	}

}
